/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package javaServices;


import entities.Members;
import entities.MyBatisUtil;
import java.util.List;
import org.apache.ibatis.session.SqlSession;

/**
 *
 * @author michael
 */
public class MemberManager{
       
    public static void insertUser(Members user) {
	  SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
	  try{
	  sqlSession.insert("mapper.MembersMapper.insertUser", user);	  
	  sqlSession.commit();
	  }finally{
	   sqlSession.close();
	  }
	 }
	 
	 public static Members getUserById(int userId) {
	  SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
	  try{
	  Members userMapper = sqlSession.selectOne("mapper.MembersMapper.getById", userId);
	  return userMapper;
	  }finally{
	   sqlSession.close();
	  }
	 }
	 
         public static Members getUserByEmail(String email) {           
             
	  SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
	  try{
	  Members userMapper = sqlSession.selectOne("mapper.MembersMapper.getUserByEmail", email);
	  return userMapper;
	  }finally{
	   sqlSession.close();
	  }
	 }
         
	 public static List<Members> getAllUsers() {
	  SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
	  try{
	  List<Members> listOfUser = sqlSession.selectList("mapper.MembersMapper.getAll");
	  return listOfUser;
	  }finally{
	   sqlSession.close();
	  }
	 }
	 
	 public static void updateUser(Members user) {
	  SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
	  try{
	  sqlSession.update("mapper.MembersMapper.updateUser", user);	  
	  sqlSession.commit();
	  }finally{
	   sqlSession.close();
	  }
	   
	 }
	 
	 public static void deleteUser(int userId) {
	  SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
	  try{
	  sqlSession.update("mapper.MembersMapper.deleteUser", userId);	  
	  sqlSession.commit();
	  }finally{
	   sqlSession.close();
	  }
         }
         
         public List<Members> searchByName(String firstNameMembers)
         {
             SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
             try{
                   List<Members> listOfUser = sqlSession.selectList("mapper.MembersMapper.searchByName" , firstNameMembers);
                   return listOfUser;
           }finally{
	   sqlSession.close();
	}
      }
         
//  public List<Members> searchByAge(int AgeMembers)
//         {
//             SqlSession sqlSession = MyBatisUtil.getSqlSessionFactory().openSession();
//             try{
//                   MembersMapper userMapper = sqlSession.getMapper(MembersMapper.class);
//                   return  userMapper.searchByAge(AgeMembers);
//           }finally{
//	   sqlSession.close();
//	}
//      }          
           
}





